package resoft.jeasyform.impl;

import resoft.jeasyform.core.MenuLoader;
import resoft.jeasyform.components.*;
import resoft.jeasyform.util.ClassUtil;
import resoft.jeasyform.event.EventHandler;

import java.util.Collection;
import java.util.Iterator;

/**
 * <p>default menu theme</p>
 * User: albert
 * Date: Aug 30, 2007
 * Time: 2:09:35 PM
 */
public class DefaultMenuLoader implements MenuLoader {

    /**
     * loadMenu
     *
     * @param parentWindow Window parent window
     * @param menu         Menu  the menu wanted to open
     */
    public void loadMenu(Window parentWindow,Menu menu) {
        //get first menuItem's position,include left and top
        if(!menu.getRootMenuItem().hasChildren()) {
            return;
        }
        Collection children = menu.getRootMenuItem().getChildren();
        int top = menu.getY();        


        //change menuItem to a button
        for(Iterator itr = children.iterator();itr.hasNext();) {
            MenuItem menuItem = (MenuItem) itr.next();

            MenuItemButton button = new MenuItemButton();

            String text = menuItem.getId() + "->" + menuItem.getText();

            button.setText(text);
            int left = menu.getX();
            button.registerClickedEvent((EventHandler) ClassUtil.newInstance(menuItem.getClickedEvent()));
            button.setRectangle(left,top,menuItem.getWidth(),1);
            parentWindow.add(button);
            top += 2;
        }
    }
}
